package com.derek.omshopping.ui.classify;

import android.util.Log;

import com.derek.omshopping.bean.classify.ClassifyLeftListViewBean;
import com.derek.omshopping.config.UrlConfig;
import com.derek.omshopping.http.RetrofitService;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * Created by My on 2016/10/17.
 */

public class ClassifyModel implements ClassifyContract.Model {
    private List<ClassifyLeftListViewBean> list_left=new ArrayList<>();//分类界面左侧list集合
    private List<String> code_mgroupAll=new ArrayList<>();



    @Override
    public void getListView(final onGetListViewUrlListener getListViewUrlListener) {
                //retrofit获取网络数据
                Retrofit retrofit=new Retrofit.Builder()
                        .baseUrl(UrlConfig.CLASSIFY_LEFT_BASEURL)
                        .build();
                Call<ResponseBody> call=retrofit.create(RetrofitService.class).getClassifyLeftListView();

                call.enqueue(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Call<ResponseBody> call, Response<ResponseBody>
                            response) {
                        try {
                            //得到json数据
                            String json=response.body().string();
                            JSONArray arr=new JSONArray(json);
                            //循环获取网络数据中的文字和图片
                            for(int i=0;i<arr.length();i++){
                                JSONObject obj=arr.getJSONObject(i);
                                String code_name=obj.getString("code_name");
                                String remark=obj.getString("remark");
                                String code_mgroup=obj.getString("code_mgroup");
                                ClassifyLeftListViewBean classifyLeftListViewBean=new ClassifyLeftListViewBean(code_name,remark);
                                //把数据添加到list集合中
                                list_left.add(classifyLeftListViewBean);

                                code_mgroupAll.add(code_mgroup);

                                Log.i("tag","集合长度为："+list_left.size());
                            }
                            getListViewUrlListener.LoadListViewUrl(list_left,code_mgroupAll);


                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(Call<ResponseBody> call, Throwable t) {
                    }
                });


    }



    public interface onGetListViewUrlListener{
        void LoadListViewUrl(List<ClassifyLeftListViewBean> list_left,List<String> code_mgroupAll);
    }

}
